package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.OnOrOff;
import com.dgut.common.QualificationGrade;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForUser;
import com.dgut.mapper.ServiceProviderMapper;
import com.dgut.mapper.UserMapper;
import com.dgut.pojo.*;
import com.dgut.pojo.receive.CheckReceive;
import com.dgut.pojo.receive.ServiceProvideReceive;
import com.dgut.service.ServiceProviderService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"all"})
@Service
public class ServiceProviderServiceImpl implements ServiceProviderService {
    @Resource
    private ServiceProviderMapper serviceProviderMapper;
    @Resource
    private UserMapper userMapper;


    /**
     * start-zwb
     */
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getUnderRiviewServiceProviderByPage(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<ServiceProvider> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        QueryWrapper<ServiceProvider> wrapper = new QueryWrapper<>();
        wrapper.eq("state",StateForUser.UNDER_REVIEW);
        Page<ServiceProvider> resultPage = serviceProviderMapper
                .selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getServiceProviderByPage(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<ServiceProvider> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        QueryWrapper<ServiceProvider> wrapper = new QueryWrapper<>();
        wrapper.in("state",StateForUser.NORMAL,StateForUser.BLOCKED);
        Page<ServiceProvider> resultPage = serviceProviderMapper
                .selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getForbidServiceProviderByPage(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<ServiceProvider> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        QueryWrapper<ServiceProvider> wrapper = new QueryWrapper<>();
        wrapper.eq("state",StateForUser.FORBID);
        Page<ServiceProvider> resultPage = serviceProviderMapper
                .selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    public Object getNormalServiceProviderByPage(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<ServiceProvider> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        QueryWrapper<ServiceProvider> wrapper = new QueryWrapper<>();
        wrapper.eq("state",StateForUser.NORMAL);
        Page<ServiceProvider> resultPage = serviceProviderMapper
                .selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object selectServiceProviderListByName(String name){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<ServiceProvider> wrapper = new QueryWrapper<>();
        if (name == null){
            List<ServiceProvider> list = serviceProviderMapper.selectList(null);
            if(list != null){
                resultMap.put("list",list);
                resultMap.put("result",true);
                resultMap.put("message","查询成功！");
            }   else {
                resultMap.put("result", false);
                resultMap.put("message", "查询失败！");
            }
        }
        else {
            wrapper.like("name", name);
            List<ServiceProvider> list = serviceProviderMapper.selectList(wrapper);
            if(list != null){
                resultMap.put("list",list);
                resultMap.put("result",true);
                resultMap.put("message","查询成功！");
            }   else {
                resultMap.put("result", false);
                resultMap.put("message", "查询失败！");
            }
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object blockServiceProvider(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(id);
        serviceProvider.setState(StateForUser.BLOCKED);
        int update = serviceProviderMapper.updateById(serviceProvider);//冻结服务商
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","用户已冻结！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","冻结失败！");
        }
        //3.将用户踢下线
        StpUtil.logoutByLoginId(id);
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object unBlockServiceProvider(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(id);
        serviceProvider.setState(StateForUser.NORMAL);
        int update = serviceProviderMapper.updateById(serviceProvider);//解冻服务商
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","用户已解冻！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","解冻失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object deleteServiceProviderById(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        int delete = serviceProviderMapper.deleteById(id);//删除服务商
        if (delete >= 0){
            resultMap.put("result",true);
            resultMap.put("message","服务商已删除！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","删除失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object checkServiceProvider(CheckReceive checkReceive) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        //1.根据id把服务商查出来
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(checkReceive.getServiceProviderId());
        //2.修改服务商的资质
        serviceProvider.setLegalAid(checkReceive.getLegalAid());
        serviceProvider.setCompanyService(checkReceive.getCompanyService());
        serviceProvider.setQualificationCertificate(checkReceive.getQualificationCertificate());
        //3.把服务商状态从审核中变为正常，即审核通过
        serviceProvider.setState(StateForUser.NORMAL);
        int update = serviceProviderMapper.updateById(serviceProvider);
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","审核通过！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","审核失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object unCheckServiceProvider(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        //1.根据id把服务商查出来
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(id);
        //2.修改服务商状态
        serviceProvider.setState(StateForUser.FORBID);
        int update = serviceProviderMapper.updateById(serviceProvider);
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","审核不通过,服务商已禁用！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","禁用失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object setQualityOrganization(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(id);
        serviceProvider.setQualityOrganization(QualificationGrade.HIGH);
        int update = serviceProviderMapper.updateById(serviceProvider);//设为优质服务商
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","服务商已设为优质！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","设置失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object unSetQualityOrganization(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(id);
        serviceProvider.setQualityOrganization(QualificationGrade.NONE);
        int update = serviceProviderMapper.updateById(serviceProvider);//设为优质服务商
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","服务商已取消优质！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","设置失败！");
        }
        return resultMap;
    }

    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    //服务商查看账户基本信息
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    public Object CheckPersonalInformation(Integer id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        ServiceProvider serviceProvider = serviceProviderMapper.selectById(id);
        resultMap.put("ServiceProviderInformation",serviceProvider);
        return resultMap;
    }

    //服务商修改账户的基本信息
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    public Object UpdatePersonalInformation(ServiceProvider serviceProvider){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        int update = serviceProviderMapper.updateById(serviceProvider);
        if (update>=1){
            resultMap.put("message","更新成功！");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("result",false);
        }
        return resultMap;
    }

    /**
     * end-lzh
     */


    /**
     * start-hwf
     */

    @Override
    public boolean updateServiceProvide(ServiceProvider serviceProvider) {
        if(serviceProviderMapper.updateById(serviceProvider) >= 1){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public ServiceProvider selectServiceProviderById(int id) {
        return serviceProviderMapper.selectById(id);
    }

    @Override
    public boolean register(ServiceProvideReceive serviceProvideReceive) {
        Map map = new Hashtable();
        map.put("username",serviceProvideReceive.getUsername());
        List<User> list = userMapper.selectByMap(map);
        if(list.size() != 0){
            return false;
        }else {
            User user = new User();

            user.setRole(SelectRole.SERVICE_PROVIDER);
            user.setUsername(serviceProvideReceive.getUsername());
            user.setPassword(serviceProvideReceive.getPassword());
            userMapper.insert(user);

            ServiceProvider serviceProvider = new ServiceProvider();

            serviceProvider.setId(user.getId());
            serviceProvider.setBranchOffice(serviceProvideReceive.getBranchOffice());
            serviceProvider.setState(StateForUser.UNDER_REVIEW);
            serviceProvider.setDirector(serviceProvideReceive.getDirector());
            serviceProvider.setEnterpriseName(serviceProvideReceive.getEnterpriseName());
            serviceProvider.setEstablishmentDate(serviceProvideReceive.getEstablishmentDate());
            serviceProvider.setCompanyNature(serviceProvideReceive.getCompanyNature());
            serviceProvider.setLegalRepresentative(serviceProvideReceive.getLegalRepresentative());
            serviceProvider.setManager(serviceProvideReceive.getManager());
            serviceProvider.setOfficeLocation(serviceProvideReceive.getOfficeLocation());
            serviceProvider.setPicture(serviceProvideReceive.getPicture());
            serviceProvider.setSupervisor(serviceProvideReceive.getSupervisor());
            serviceProvider.setShareholder(serviceProvideReceive.getShareholder());
            serviceProvider.setRegistrationPlace(serviceProvideReceive.getRegistrationPlace());
            serviceProvider.setRegisteredCapital(serviceProvideReceive.getRegisteredCapital());
            serviceProvider.setLegalAid(QualificationGrade.NONE);
            serviceProvider.setQualityOrganization(QualificationGrade.NONE);
            serviceProvider.setQualificationCertificate(QualificationGrade.NONE);
            serviceProvider.setCompanyService(QualificationGrade.NONE);
            serviceProvider.setInvestmentAmount(serviceProvideReceive.getInvestmentAmount());

            serviceProviderMapper.insert(serviceProvider);

            return true;
        }
    }
    /**
     * end-hwf
     */

    /**
     * start-hqq
     */
    /**
     *
     * @param pageRequestInfo
     * @return
     */
    @Override
    public Object getQualityServiceProvider(PageRequestInfo pageRequestInfo){
        QueryWrapper<ServiceProvider> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("quality_organization", QualificationGrade.HIGH);
        Page<ServiceProvider> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<ServiceProvider> resultPage = serviceProviderMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        ConcurrentHashMap map = new ConcurrentHashMap();
        map.put("qualityServiceProvider",result);
        return map;
    }
    @Override
    public Object getQualityServiceProviderByLocation(PageRequestInfo pageRequestInfo,String location){
        QueryWrapper<ServiceProvider> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("quality_organization", QualificationGrade.HIGH);
        if(location!=null){
            queryWrapper.like("office_location",location);
        }
        Page<ServiceProvider> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<ServiceProvider> resultPage = serviceProviderMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        ConcurrentHashMap map = new ConcurrentHashMap();
        map.put("qualityServiceProvider",result);
        return map;
    }
    /**
     * end-hqq
     */
}
